/**
 * @Description 
 * @Author LZL
 * @Date 2025/2/25
 */



package com.kcb.usersmanager;

import com.kcb.connectDB.connectDB;
import com.kcb.login.login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class usersmanager extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton addButton, deleteButton, backButton;

    public usersmanager() {
        setTitle("城科课程表管理系统:用户管理");
        setSize(450, 350);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        ImageIcon icon = new ImageIcon("C:\\Users\\TBS\\IdeaProjects\\KCB\\image\\ccst.png");
        if (icon != null) {
            setIconImage(icon.getImage());
        } else {
            System.out.println("Failed to load icon image.");
        }

        // 创建主面板
        JPanel panel = new JPanel();
        panel.setBackground(new Color(230, 240, 250));
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JLabel titleLabel = new JLabel("用户管理");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 22));
        titleLabel.setForeground(new Color(50, 50, 150));

        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font("宋体", Font.PLAIN, 16));

        usernameField = new JTextField(15);
        usernameField.setFont(new Font("宋体", Font.PLAIN, 14));

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font("宋体", Font.PLAIN, 16));

        passwordField = new JPasswordField(15);
        passwordField.setFont(new Font("宋体", Font.PLAIN, 14));
        passwordField.setEchoChar('*');

        addButton = createStyledButton("添加用户", new Color(60, 180, 75));
        deleteButton = createStyledButton("删除用户", new Color(220, 50, 50));
        backButton = createStyledButton("返回登录", new Color(50, 100, 200));

        // 添加组件到布局
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(titleLabel, gbc);

        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        panel.add(usernameField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        panel.add(passwordField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(addButton, gbc);

        gbc.gridx = 1;
        panel.add(deleteButton, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(backButton, gbc);

        getContentPane().add(panel);

        // 按钮监听器
        addButton.addActionListener(e -> handleUserAction("add"));
        deleteButton.addActionListener(e -> handleUserAction("delete"));
        backButton.addActionListener(e -> {
            new login().setVisible(true);
            dispose();
        });
    }

    private JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text);
        button.setFont(new Font("宋体", Font.BOLD, 14));
        button.setBackground(bgColor);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setPreferredSize(new Dimension(120, 40));
        return button;
    }

    private void handleUserAction(String action) {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());

        if (username.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户名和密码不能为空！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        connectDB dbConnector = new connectDB();
        boolean success;
        if ("add".equals(action)) {
            success = dbConnector.addUser(username, password);
            JOptionPane.showMessageDialog(this, success ? "用户添加成功！" : "添加用户失败。", "操作结果",
                    success ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE);
        } else {
            success = dbConnector.deleteUser(username, password);
            JOptionPane.showMessageDialog(this, success ? "用户删除成功！" : "删除用户失败。", "操作结果",
                    success ? JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE);
        }

        if (success) dispose();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new usersmanager().setVisible(true));
    }
}
